package com.resourcestore.action;

import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.MajorGroupDao;
import com.resourcestore.model.MajorGroup;

public class MajorGroupAction extends BaseAction {

	private MajorGroup majorGroup;
	private List<MajorGroup> listMajorGroup;
	private MajorGroupDao majorGroupDao = new MajorGroupDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */

	public String listMajorGroup() {
		majorGroup = new MajorGroup();
		pagination.setTotal(majorGroupDao.queryMajorGroupNum(majorGroup));
		listMajorGroup = majorGroupDao.selectMajorGroupPage(majorGroup,
				pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listMajorGroup.size(); i++) {
			try {
				listMajorGroup.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listMajorGroup.get(i)
								.getGmtCreated()));
				listMajorGroup.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listMajorGroup.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListMajorGroup() {

		// 对内容进行urldecoder解码
		try {
			majorGroup.setMajorgroupName(new String(majorGroup
					.getMajorgroupName().getBytes("iso-8859-1"), "UTF-8"));
			majorGroup.setMajorgroupRemark(new String(majorGroup
					.getMajorgroupRemark().getBytes("iso-8859-1"), "UTF-8"));
			majorGroup.setAuthor(new String(majorGroup.getAuthor().getBytes(
					"iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(majorGroupDao.queryMajorGroupNum(majorGroup));
		listMajorGroup = majorGroupDao.selectMajorGroupPage(majorGroup,
				pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listMajorGroup.size(); i++) {
			try {
				listMajorGroup.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listMajorGroup.get(i)
								.getGmtCreated()));
				listMajorGroup.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listMajorGroup.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/MajorGroup/listMajorGroup.jsp");
	}

	/**
	 * 预插入一条数据
	 */
	public String preInsertMajorGroup() {

		return newpath("/MajorGroup/insertMajorGroup.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertMajorGroup() {

		try {
			majorGroup.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			majorGroup.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		majorGroupDao.insertMajorGroup(majorGroup);
		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateMajorGroup() {
		majorGroup = majorGroupDao.selectMajorGroupById(majorGroup);

		return newpath("/MajorGroup/updateMajorGroup.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateMajorGroup() {

		try {
			// majorGroup.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			majorGroup.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		majorGroupDao.updateMajorGroup(majorGroup);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteMajorGroup() {
		majorGroupDao.deleteMajorGroup(majorGroup);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreMajorGroup() {
		
		majorGroupDao.deleteMoreMajorGroup(majorGroup);
		return redirect(this.getUrl());
	}

	public MajorGroup getMajorGroup() {
		return majorGroup;
	}

	public void setMajorGroup(MajorGroup majorGroup) {
		this.majorGroup = majorGroup;
	}

	public List<MajorGroup> getListMajorGroup() {
		return listMajorGroup;
	}

	public void setListMajorGroup(List<MajorGroup> listMajorGroup) {
		this.listMajorGroup = listMajorGroup;
	}

	public Pagination getPagination() {
		return this.pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
}
